﻿using System.FileServer.ApiModels;

using GCC.Container.Abstracts;

using GCCGrpc.FileServer;

using Grpc.Core;

namespace System.FileServer
{
    [GCCGRPCServer]
    public class FileGrpcServer : FileServerGrpc.FileServerGrpcBase
    {

        public override Task<GRPCCreateBigFileResponse> GRPCCreateBigFile(GRPCCreateBigFileRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var dbRes = FileManager.Instance.SaveFile(request, out var file);
                if (file is null)
                {
                    return new GRPCCreateBigFileResponse();
                }
                var res = new GRPCCreateBigFileResponse()
                {
                    CheckCode = file.CheckCode,
                    CurrentName = file.CurrentName,
                    CurrentSavePoint = file.CurrentPointName,
                    ID = file.FID,
                    OriginalName = file.OriginalName,
                };
                return res;
            });
        }

        public override Task<GRPCCreateFilesResponse> GRPCCreateFiles(GRPCCreateFilesRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var res = new GRPCCreateFilesResponse();
                var dbRes = FileManager.Instance.SaveFiles(request, out var files);
                files.ForEach(item =>
                {
                    res.Items.Add(new GRPCCreateFilesResponseItem()
                    {
                        CheckCode = item.CheckCode,
                        CurrentName = item.CurrentName,
                        CurrentSavePoint = item.CurrentPointName,
                        ID = item.FID,
                        OriginalName = item.OriginalName,
                    });
                });
                return res;
            });
        }

        public override Task<GRPCGetFilesByCheckCodeResponse> GRPCGetFilesByCheckCode(GRPCGetFilesByCheckCodeRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var res = new GRPCGetFilesByCheckCodeResponse();
                var dbRes = FileManager.Instance.SearchByCheckCode(request.CheckCode);
                dbRes.ForEach(item =>
                {
                    res.Items.Add(new GRPCGetFilesByCheckCodeResponseItem()
                    {
                        CheckCode = item.CheckCode,
                        CreatedBy = item.CreatedBy,
                        Created = item.Created.ToFileTimeUtc(),
                        CurrentName = item.CurrentName,
                        CurrentSavePoint = item.CurrentPointName,
                        ID = item.FID,
                        Modified = item.Modified.ToFileTimeUtc(),
                        ModifiedBy = item.ModifiedBy,
                        OriginalName = item.OriginalName,
                    });
                });
                return res;
            });
        }

        public override Task<GRPCGetAllItemsResponse> GRPCGetAllItems(GRPCGetAllItemsRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var res = new GRPCGetAllItemsResponse();
                var dbRes = FileManager.Instance.SearchItems(
                    new GetAllItemsRequest()
                    {
                        PageIndex = request.PageIndex,
                        PageSize = request.PageSize
                    }
                    , out var count
                    );
                dbRes.ForEach(item =>
                {
                    res.Items.Add(new GRPCGetAllItemsResponseItem()
                    {
                        CheckCode = item.CheckCode,
                        CreatedBy = item.CreatedBy,
                        Created = item.Created.ToFileTimeUtc(),
                        CurrentName = item.CurrentName,
                        CurrentSavePoint = item.CurrentPointName,
                        ID = item.FID,
                        Modified = item.Modified.ToFileTimeUtc(),
                        ModifiedBy = item.ModifiedBy,
                        OriginalName = item.OriginalName,
                    });
                });
                res.DataCount = count;
                return res;
            });
        }

        public override Task<GRPCGetFileByIDResponse> GRPCGetFileByID(GRPCGetFileByIDRequest request, ServerCallContext context)
        {
            return Task.Run(() =>
            {
                var dbres = FileManager.Instance.SearchByID(request.FID);
                if (dbres is null)
                {
                    return new GRPCGetFileByIDResponse();
                }
                return new GRPCGetFileByIDResponse()
                {
                    CheckCode = dbres.CheckCode,
                    Created = dbres.Created.ToFileTimeUtc(),
                    CreatedBy = dbres.CreatedBy,
                    CurrentName = dbres.CurrentName,
                    CurrentSavePoint = dbres.CurrentPointName,
                    ID = dbres.FID,
                    ModifiedBy = dbres.ModifiedBy,
                    Modified = dbres.Modified.ToFileTimeUtc(),
                    OriginalName = dbres.OriginalName,
                    FilePath = dbres.FileSavePath
                };
            });
        }
    }
}
